const bcrypt = require('bcryptjs')

// 加密
exports.hash = (myPlaintextPassword) => {
    return new Promise((resolve, reject) => {
        //第二个参数是盐
        bcrypt.hash(myPlaintextPassword, 10, function(err, hash) {
            if (err) {
                reject(err)
            } else {
                //成功则返回加密后的hash值
                resolve(hash)
            }
        })
    })
}

// 密码比较
exports.compare = (myPlaintextPassword, hash) => {
    return new Promise((resolve, reject) => {
        bcrypt.compare(myPlaintextPassword, hash, (err, result) => {
            if (err) {
                reject(err)
            } else {
                resolve(result)
            }
        })
    })
}